package org.edu.jgame.cardgames;

import java.awt.Font;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.newdawn.slick.*;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.*;

public class ConnectState extends BasicGameState {

	int stateID = -1;
	
	Image background = null;
	Image backButton = null;
	Image[] connectButton = null;
	
	float backx = 0;
	float backy = 0;
	float connectx = 0;
	float connecty = 0;
	
	UnicodeFont unicodeFont = null;
	String name = "Player";
	String host = "";
	boolean connected = false;
	
	int focus = 0;
	int idx_connect = 0;
	
	public ConnectState(int id) {
		stateID = id;
	}
	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {
		background = new Image("data/Connect.png");
		backButton = new Image("data/BackButton.png");
		
		connectButton = new Image[2];
		connectButton[0] = new Image("data/ConnectButton.png");
		connectButton[1] = new Image("data/ConnectButtonPress.png");
		
		backx = 64;
		backy = 481;
		connectx = 558;
		connecty = 362;
		
		unicodeFont = new UnicodeFont(new Font("Arial", Font.BOLD, 25));
		unicodeFont.addAsciiGlyphs();
		unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		unicodeFont.loadGlyphs();
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
			throws SlickException {
		background.draw();
		backButton.draw(backx, backy);
		
		connectButton[idx_connect].draw(connectx, connecty);
		if(focus == 0) {
			unicodeFont.drawString(130, 195, name + "_");
			unicodeFont.drawString(130, 315, host);
		}
		else {
			unicodeFont.drawString(130, 195, name);
			unicodeFont.drawString(130, 315, host + "_");
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		Input input = gc.getInput();
		
		try {
			if(is.available() > 0) {
				ResponseLine = is.readLine();
			}
		}
		catch (Exception e) {
			
		}
	
		if((input.getMouseX() >= backx && input.getMouseX() <= backx + backButton.getWidth()) 
				&& (input.getMouseY() >= backy && input.getMouseY() <= backy + backButton.getHeight())) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && !connected) {
				sbg.enterState(JCardGame.MAINMENUSTATE);
			}
		}
		
		if((input.getMouseX() >= connectx && input.getMouseX() <= connectx + connectButton[0].getWidth()) 
				&& (input.getMouseY() >= connecty && input.getMouseY() <= connecty + connectButton[0].getHeight())) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && !connected) {
				connected = true;
				connect();
				idx_connect = 1;
			}
			else {
				idx_connect = 0;
			}
		}
		
		if((input.getMouseX() >= 126 && input.getMouseX() <= 126 + 537) 
				&& (input.getMouseY() >= 185 && input.getMouseY() <= 185 + 61)) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				focus = 0;
			}
		}
		
		if((input.getMouseX() >= 126 && input.getMouseX() <= 126 + 537) 
				&& (input.getMouseY() >= 301 && input.getMouseY() <= 301 + 61)) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				focus = 1;
			}
		}
		
		if(input.isKeyPressed(Input.KEY_0)) {
			if(focus == 0 && name.length() <= 10) {
				name += "0";
			}
			else {
				host += "0";
			}
		}
		if(input.isKeyPressed(Input.KEY_1)) {
			if(focus == 0 && name.length() <= 10) {
				name += "1";
			}
			else {
				host += "1";
			}
		}
		if(input.isKeyPressed(Input.KEY_2)) {
			if(focus == 0 && name.length() <= 10) {
				name += "2";
			}
			else {
				host += "2";
			}
		}
		if(input.isKeyPressed(Input.KEY_3)) {
			if(focus == 0 && name.length() <= 10) {
				name += "3";
			}
			else {
				host += "3";
			}
		}
		if(input.isKeyPressed(Input.KEY_4)) {
			if(focus == 0 && name.length() <= 10) {
				name += "4";
			}
			else {
				host += "4";
			}
		}
		if(input.isKeyPressed(Input.KEY_5)) {
			if(focus == 0 && name.length() <= 10) {
				name += "5";
			}
			else {
				host += "5";
			}
		}
		if(input.isKeyPressed(Input.KEY_6)) {
			if(focus == 0 && name.length() <= 10) {
				name += "6";
			}
			else {
				host += "6";
			}
		}
		if(input.isKeyPressed(Input.KEY_7)) {
			if(focus == 0 && name.length() <= 10) {
				name += "7";
			}
			else {
				host += "7";
			}
		}
		if(input.isKeyPressed(Input.KEY_8)) {
			if(focus == 0 && name.length() <= 10) {
				name += "8";
			}
			else {
				host += "8";
			}
		}
		if(input.isKeyPressed(Input.KEY_9)) {
			if(focus == 0 && name.length() <= 10) {
				name += "9";
			}
			else {
				host += "9";
			}
		}
		if(input.isKeyPressed(Input.KEY_Q)) {
			if(focus == 0 && name.length() <= 10) {
				name += "Q";
			}
			else {
				host += "q";
			}
		}
		if(input.isKeyPressed(Input.KEY_W)) {
			if(focus == 0 && name.length() <= 10) {
				name += "W";
			}
			else {
				host += "w";
			}
		}
		if(input.isKeyPressed(Input.KEY_E)) {
			if(focus == 0 && name.length() <= 10) {
				name += "E";
			}
			else {
				host += "e";
			}
		}
		if(input.isKeyPressed(Input.KEY_R)) {
			if(focus == 0 && name.length() <= 10) {
				name += "R";
			}
			else {
				host += "r";
			}
		}
		if(input.isKeyPressed(Input.KEY_T)) {
			if(focus == 0 && name.length() <= 10) {
				name += "T";
			}
			else {
				host += "t";
			}
		}
		if(input.isKeyPressed(Input.KEY_Y)) {
			if(focus == 0 && name.length() <= 10) {
				name += "Y";
			}
			else {
				host += "y";
			}
		}
		if(input.isKeyPressed(Input.KEY_U)) {
			if(focus == 0 && name.length() <= 10) {
				name += "U";
			}
			else {
				host += "u";
			}
		}
		if(input.isKeyPressed(Input.KEY_I)) {
			if(focus == 0 && name.length() <= 10) {
				name += "I";
			}
			else {
				host += "i";
			}
		}
		if(input.isKeyPressed(Input.KEY_O)) {
			if(focus == 0 && name.length() <= 10) {
				name += "O";
			}
			else {
				host += "o";
			}
		}
		if(input.isKeyPressed(Input.KEY_P)) {
			if(focus == 0 && name.length() <= 10) {
				name += "P";
			}
			else {
				host += "p";
			}
		}
		if(input.isKeyPressed(Input.KEY_A)) {
			if(focus == 0 && name.length() <= 10) {
				name += "A";
			}
			else {
				host += "a";
			}
		}
		if(input.isKeyPressed(Input.KEY_S)) {
			if(focus == 0 && name.length() <= 10) {
				name += "S";
			}
			else {
				host += "s";
			}
		}
		if(input.isKeyPressed(Input.KEY_D)) {
			if(focus == 0 && name.length() <= 10) {
				name += "D";
			}
			else {
				host += "d";
			}
		}
		if(input.isKeyPressed(Input.KEY_F)) {
			if(focus == 0 && name.length() <= 10) {
				name += "F";
			}
			else {
				host += "f";
			}
		}
		if(input.isKeyPressed(Input.KEY_G)) {
			if(focus == 0 && name.length() <= 10) {
				name += "G";
			}
			else {
				host += "g";
			}
		}
		if(input.isKeyPressed(Input.KEY_H)) {
			if(focus == 0 && name.length() <= 10) {
				name += "H";
			}
			else {
				host += "h";
			}
		}
		if(input.isKeyPressed(Input.KEY_J)) {
			if(focus == 0 && name.length() <= 10) {
				name += "J";
			}
			else {
				host += "j";
			}
		}
		if(input.isKeyPressed(Input.KEY_K)) {
			if(focus == 0 && name.length() <= 10) {
				name += "K";
			}
			else {
				host += "k";
			}
		}		
		if(input.isKeyPressed(Input.KEY_L)) {
			if(focus == 0 && name.length() <= 10) {
				name += "L";
			}
			else {
				host += "l";
			}
		}
		if(input.isKeyPressed(Input.KEY_Z)) {
			if(focus == 0 && name.length() <= 10) {
				name += "Z";
			}
			else {
				host += "z";
			}
		}
		if(input.isKeyPressed(Input.KEY_X)) {
			if(focus == 0 && name.length() <= 10) {
				name += "X";
			}
			else {
				host += "x";
			}
		}
		if(input.isKeyPressed(Input.KEY_C)) {
			if(focus == 0 && name.length() <= 10) {
				name += "C";
			}
			else {
				host += "c";
			}
		}
		if(input.isKeyPressed(Input.KEY_V)) {
			if(focus == 0 && name.length() <= 10) {
				name += "V";
			}
			else {
				host += "v";
			}
		}
		if(input.isKeyPressed(Input.KEY_B)) {
			if(focus == 0 && name.length() <= 10) {
				name += "B";
			}
			else {
				host += "b";
			}
		}
		if(input.isKeyPressed(Input.KEY_N)) {
			if(focus == 0 && name.length() <= 10) {
				name += "N";
			}
			else {
				host += "n";
			}
		}
		if(input.isKeyPressed(Input.KEY_M)) {
			if(focus == 0 && name.length() <= 10) {
				name += "M";
			}
			else {
				host += "m";
			}
		}
		if(input.isKeyPressed(Input.KEY_BACK)) {
			if(focus == 0 ) {
				name = name.substring(0, name.length()-1);
			}
			else {
				host = host.substring(0, host.length()-1);
			}
		}
		
		
		if(ResponseLine.equalsIgnoreCase("shuffle")) {
			GameplayState.SetNetwork(clientSocket, os, is, closed);
			sbg.enterState(JCardGame.GAMEPLAYSTATE);
		}
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return stateID;
	}
	
	// Network part
	static Socket clientSocket = null;
	static PrintStream os = null;
	static DataInputStream is = null;
	static BufferedReader inputLine = null;
	static boolean closed = true;
	static String ResponseLine = "";
	
	public void connect() {
		int port_number = 2224;
		//String host = "localhost";

		System.out.println("Usage: java MultiThreadChatClient  \n"
				+ "Now using host=" + host + ", port_number=" + port_number);
		
		// Initialization section:
		// Try to open a socket on a given host and port
		// Try to open input and output streams
		try {
			clientSocket = new Socket(host, port_number);
			closed = false;
			
			inputLine = new BufferedReader(new InputStreamReader(System.in));
			os = new PrintStream(clientSocket.getOutputStream());
			is = new DataInputStream(clientSocket.getInputStream());
			
			os.println("name " + name);
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host " + host);
		} catch (IOException e) {
			System.err
					.println("Couldn't get I/O for the connection to the host "
							+ host);
		}
			
	}

}
